package com.ln.adoms.action;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ln.adoms.entity.Marchandise;
import com.ln.adoms.entity.StockDetail;
import com.ln.adoms.service.MarchandiseService;
import com.ln.adoms.service.StockDetailService;
import com.ln.core.orm.Filtration;
import com.ln.core.orm.Filtration.MatchType;
import com.ln.core.orm.PageData;
import com.ln.core.utils.WebUtils;

@Controller
@RequestMapping("/adoms/stockDetail")
public class StockDetailAction extends BaseAction {

	@Resource
	private StockDetailService stockDetailService;
	@Resource
	private MarchandiseService marchandiseService;

	@RequestMapping("/list/{mid}")
	public String list(HttpServletRequest request, @PathVariable("mid") Integer mid, Model model) {
		PageData<StockDetail> pageData = new PageData<StockDetail>();
		// 给pageData设置参数
		WebUtils.setPageDataParameter(request, pageData);
		pageData.getFiltrations().add(new Filtration(MatchType.EQ, mid, "marchandise.id"));
		pageData.getFiltrations().add(new Filtration(MatchType.EQ, this.getCurrentDepartmentId(request), "department.id"));
		pageData = stockDetailService.find(pageData);

		Marchandise m = marchandiseService.find(mid);
		model.addAttribute("m", m);

		model.addAttribute("pageData", pageData);

		return "/adoms/goods/stockDetail";
	}

	@RequestMapping(value = "/detail/{mid}")
	public String stockDetail(Model model, HttpServletRequest request, @PathVariable("mid") Integer mid) {
		String hql = "from StockDetail where marchandise.id=? and department.id = ?";
		List<StockDetail> detailList = stockDetailService.findList(hql, mid, this.getCurrentDepartmentId(request));
		Marchandise m = marchandiseService.find(mid);
		model.addAttribute("m", m);
		model.addAttribute("detailList", detailList);
		return "/adoms/goods/stockDetail";
	}

}
